package com.hdsx.jxcsxm.funds.controller;
import com.hdsx.jxcsxm.funds.bean.FundBean;
import com.hdsx.jxcsxm.funds.bean.ProcessorBean;
import com.hdsx.jxcsxm.funds.bean.ProcessorParam;
import com.hdsx.jxcsxm.funds.server.FundServer;
import com.hdsx.jxcsxm.jhcx.server.impl.JhcxServerImpl;
import com.hdsx.jxcsxm.tjbb.bean.ExcelData;
import com.hdsx.jxcsxm.tjbb.bean.Excel_export;
import com.hdsx.jxcsxm.tjbb.bean.Excel_list;
import com.hdsx.jxcsxm.tjbb.bean.Excel_tilte;
import com.hdsx.jxcsxm.utile.*;
import com.hdsx.jxcsxm.utile.excel.ExcelCoordinate;
import com.hdsx.jxcsxm.utile.excel.ExcelEntity;
import com.hdsx.jxcsxm.utile.excel.ExcelExportUtil;
import com.hdsx.jxcsxm.utile.excel.ExcelTitleCell;
import com.hdsx.jxcsxm.xtgl.bean.TreeNode;
import com.hdsx.jxcsxm.xtgl.bean.Xmjbxx;
import com.hdsx.jxcsxm.zjbf.bean.XmZjbf;
import com.hdsx.jxcsxm.zjdw.bean.XmZjdw;
import com.hdsx.webutil.struts.BaseActionSupport;
import com.opensymphony.xwork2.ModelDriven;
import org.apache.poi.ss.usermodel.IndexedColors;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.PrintWriter;
import java.math.BigDecimal;
import java.util.*;

/**
 * @author 占贤银
 * time 2024/2/2 11:00
 * description 生命之灯因热情而点燃，生命之舟因拼搏而前行
 */
@Scope("prototype")
@Controller
public class FundController extends BaseActionSupport implements ModelDriven<FundBean> {

    @Resource(name = "fundServerImpl")
    private FundServer fundServer;
    private File fileupload;
    private String fileuploadFileName;

    private FundBean fundBean = new FundBean();

    private Xmjbxx xmjbxx=new Xmjbxx();

    private XmZjdw xmZjdw=new XmZjdw();

    private String sbthcd;

    public String getSbthcd() {
        return sbthcd;
    }

    public void setSbthcd(String sbthcd) {
        this.sbthcd = sbthcd;
    }

    public XmZjdw getXmZjdw() {
        return xmZjdw;
    }

    public void setXmZjdw(XmZjdw xmZjdw) {
        this.xmZjdw = xmZjdw;
    }

    public File getFileupload() {
        return fileupload;
    }

    public void setFileupload(File fileupload) {
        this.fileupload = fileupload;
    }

    public String getFileuploadFileName() {
        return fileuploadFileName;
    }

    public void setFileuploadFileName(String fileuploadFileName) {
        this.fileuploadFileName = fileuploadFileName;
    }

    public Xmjbxx getXmjbxx() {
        return xmjbxx;
    }

    public void setXmjbxx(Xmjbxx xmjbxx) {
        this.xmjbxx = xmjbxx;
    }

    public FundBean getFundBean() {
        return fundBean;
    }

    public void setFundBean(FundBean fundBean) {
        this.fundBean = fundBean;
    }

    public void queryjhDataBybgDel() {
        fundBean.setXzqh(MyUtil.getQueryTJ(fundBean.getXzqh(), "xzqhdm"));
        fundBean.setXmnf(MyUtil.getQueryTJ(fundBean.getXmnf(), "xmnf"));
        fundBean.setJsxz(MyUtil.getQueryTJ(fundBean.getJsxz(), "jsxz"));
        fundBean.setZjly(MyUtil.getQueryTJ(fundBean.getZjly(), "zjly"));
        fundBean.setGydwdm(MyUtil.getQueryTJDW(fundBean.getGydwdm(), "gydwdm"));
        List<ProcessorBean> list = fundServer.queryjhDataBybgDel(fundBean);
        int count = fundServer.queryjhDataBybgDelCount(fundBean);
        EasyUIPage<ProcessorBean> e = new EasyUIPage<ProcessorBean>();
        e.setRows(list);
        e.setTotal(count);
        try {
            JsonUtils.write(e, getresponse().getWriter());
        } catch (Exception e1) {
            e1.printStackTrace();
        }
    }

    public void queryZjdwXmlist() {
        fundBean.setXzqh(MyUtil.getQueryTJ(fundBean.getXzqh(), "xzqhdm"));
        fundBean.setXmnf(MyUtil.getQueryTJ(fundBean.getXmnf(), "xmnf"));
        fundBean.setJhxdwh(MyUtil.getQueryTJIN(fundBean.getJhxdwh(), "jhxdwh"));
        fundBean.setShzt(MyUtil.getQueryTJ(fundBean.getShzt(), "shztstr"));
        fundBean.setSsbzt(MyUtil.getQueryTJ(fundBean.getSsbzt(), "ssbztstr"));
        fundBean.setXsbzt(MyUtil.getQueryTJ(fundBean.getXsbzt(), "xsbztstr"));
        List<FundBean> list = fundServer.queryZjdwXmlist(fundBean);
        int count = fundServer.queryZjdwXmlistCount(fundBean);
        EasyUIPage<FundBean> e=new EasyUIPage<FundBean>();
        e.setRows(list);
        e.setTotal(count);
        try {
            JsonUtils.write(e, getresponse().getWriter());
        } catch (Exception e1) {
            e1.printStackTrace();
        }
    }

    public void getZjdwTjAll() {
        try {
            fundBean.setXzqh(MyUtil.getQueryTJ(fundBean.getXzqh(), "xzqhdm"));
            fundBean.setXmnf(MyUtil.getQueryTJ(fundBean.getXmnf(), "xmnf"));
            fundBean.setJhxdwh(MyUtil.getQueryTJIN(fundBean.getJhxdwh(), "jhxdwh"));
            fundBean.setShzt(MyUtil.getQueryTJ(fundBean.getShzt(), "shztstr"));
            fundBean.setSsbzt(MyUtil.getQueryTJ(fundBean.getSsbzt(), "ssbztstr"));
            fundBean.setXsbzt(MyUtil.getQueryTJ(fundBean.getXsbzt(), "xsbztstr"));
            JsonUtils.write(fundServer.getZjdwTjAll(fundBean), getresponse().getWriter());
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void getTjAll() {
        try {
            fundBean.setXzqh(MyUtil.getQueryTJ(fundBean.getXzqh(), "xzqhdm"));
            fundBean.setXmnf(MyUtil.getQueryTJ(fundBean.getXmnf(), "xmnf"));
            fundBean.setJhxdwh(MyUtil.getQueryTJIN(fundBean.getJhxdwh(), "jhxdwh"));
            fundBean.setShzt(MyUtil.getQueryTJ(fundBean.getShzt(), "shztstr"));
            fundBean.setSsbzt(MyUtil.getQueryTJ(fundBean.getSsbzt(), "ssbztstr"));
            fundBean.setXsbzt(MyUtil.getQueryTJ(fundBean.getXsbzt(), "xsbztstr"));
            fundBean.setSfqbbf(MyUtil.getQueryTJ(fundBean.getSfqbbf(), "sfqbbf"));
            JsonUtils.write(fundServer.getTjAll(fundBean), getresponse().getWriter());
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void queryXmlist() {
        fundBean.setXzqh(MyUtil.getQueryTJ(fundBean.getXzqh(), "xzqhdm"));
        fundBean.setXmnf(MyUtil.getQueryTJ(fundBean.getXmnf(), "xmnf"));
        fundBean.setJhxdwh(MyUtil.getQueryTJIN(fundBean.getJhxdwh(), "jhxdwh"));
        fundBean.setShzt(MyUtil.getQueryTJ(fundBean.getShzt(), "shztstr"));
        fundBean.setSsbzt(MyUtil.getQueryTJ(fundBean.getSsbzt(), "ssbztstr"));
        fundBean.setXsbzt(MyUtil.getQueryTJ(fundBean.getXsbzt(), "xsbztstr"));
        fundBean.setSfqbbf(MyUtil.getQueryTJ(fundBean.getSfqbbf(), "sfqbbf"));
        List<FundBean> list = fundServer.queryXmlist(fundBean);
        int count = fundServer.queryXmlistCount(fundBean);
        EasyUIPage<FundBean> e=new EasyUIPage<FundBean>();
        e.setRows(list);
        e.setTotal(count);
        try {
            JsonUtils.write(e, getresponse().getWriter());
        } catch (Exception e1) {
            e1.printStackTrace();
        }
    }

    public void exportXmList() {
        List<Excel_list> list = null;
        try {
            fundBean.setXzqh(MyUtil.getQueryTJ(fundBean.getXzqh(), "xzqhdm"));
            fundBean.setXmnf(MyUtil.getQueryTJ(fundBean.getXmnf(), "xmnf"));
            fundBean.setJhxdwh(MyUtil.getQueryTJIN(fundBean.getJhxdwh(), "jhxdwh"));
            ExcelData eldata=new ExcelData();
            eldata.setTitleName("切块资金资金情况明细表");
            eldata.setSheetName("切块资金资金情况明细表");
            eldata.setFileName("切块资金资金情况明细表");
            List<Excel_tilte> et=new ArrayList<Excel_tilte>();
            list = fundServer.exportXmList(fundBean);
            // list = tjbbServer.exportZjxdzjdwzjbf(elist);
            et.add(new Excel_tilte("管养单位",1,2,0,0));
            et.add(new Excel_tilte("项目名称",1,2,1,1));
            et.add(new Excel_tilte("下达计划情况",1,1,2,5));
            et.add(new Excel_tilte("资金到位情况",1,1,6,9));
            et.add(new Excel_tilte("资金拨付情况",1,1,10,13));
            et.add(new Excel_tilte("资金结余情况",1,1,14,17));
            et.add(new Excel_tilte("小计",2,2,2,2));
            et.add(new Excel_tilte("部补（万元）",2,2,3,3));
            et.add(new Excel_tilte("省补（万元）",2,2,4,4));
            et.add(new Excel_tilte("地方自筹（万元）",2,2,5,5));
            et.add(new Excel_tilte("小计",2,2,6,6));
            et.add(new Excel_tilte("部补（万元）",2,2,7,7));
            et.add(new Excel_tilte("省补（万元）",2,2,8,8));
            et.add(new Excel_tilte("地方自筹（万元）",2,2,9,9));
            et.add(new Excel_tilte("小计",2,2,10,10));
            et.add(new Excel_tilte("部补（万元）",2,2,11,11));
            et.add(new Excel_tilte("省补（万元）",2,2,12,12));
            et.add(new Excel_tilte("地方自筹（万元）",2,2,13,13));
            et.add(new Excel_tilte("小计",2,2,14,14));
            et.add(new Excel_tilte("部补（万元）",2,2,15,15));
            et.add(new Excel_tilte("省补（万元）",2,2,16,16));
            et.add(new Excel_tilte("地方自筹（万元）",2,2,17,17));
            eldata.setEl(list);
            // 将表头内容设置到类里面
            eldata.setEt(et);
            HttpServletResponse response= getresponse();
            Excel_export.excel_export(eldata,response);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (list != null) {
                list = null;
            }
        }
    }

    public void getxmInfo() {
        FundBean results = fundServer.getxmInfo(fundBean);
        try {
            JsonUtils.write(results, getresponse().getWriter());
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void queryzjxdlist() {
        if("0".equals(fundBean.getSffy())){
            fundBean.setPage(1);
            fundBean.setRows(500);
        }
        List<FundBean> list=fundServer.queryzjxdlist(fundBean);
        int count=fundServer.queryzjxdlistCount(fundBean);
        EasyUIPage<FundBean> e=new EasyUIPage<FundBean>();
        e.setRows(list);
        e.setTotal(count);
        try {
            JsonUtils.write(e, getresponse().getWriter());
        } catch (Exception e1) {
            e1.printStackTrace();
        }
    }

    public void getdwTj() {
        try {
            JsonUtils.write(fundServer.getdwTj(fundBean), getresponse().getWriter());
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void getJhxdByWh() {
        try {
            FundBean list = fundServer.getJhxdByWh(fundBean);
            JsonUtils.write(list, getresponse().getWriter());
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void getbfTj(){
        try {
            JsonUtils.write(fundServer.getbfTj(fundBean), getresponse().getWriter());
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void getFundjhxdwh() {
        try {
            JsonUtils.write(fundServer.getFundjhxdwh(fundBean), getresponse().getWriter());
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void zjbfCcdDcmb() {
        ExcelTitleCell[] title = null;
        try {
            title = new ExcelTitleCell[23];
            title[0] = new ExcelTitleCell("项目编码", false,
                    new ExcelCoordinate(0, (short) 0), null, 20,false);
            title[1] = new ExcelTitleCell("项目年份", false,
                    new ExcelCoordinate(0, (short) 1), null, 10,false);
            title[2] = new ExcelTitleCell("项目名称", false,
                    new ExcelCoordinate(0, (short) 2), null, 20,false);
            title[3] = new ExcelTitleCell("管养单位", false,
                    new ExcelCoordinate(0, (short) 3), null, 20,false);
            title[4] = new ExcelTitleCell("行政区划", false,
                    new ExcelCoordinate(0, (short) 4), null, 20,false);
            title[5] = new ExcelTitleCell("计划下达资金(万元)", false,
                    new ExcelCoordinate(0, (short) 5), null, 20,false);
            title[6] = new ExcelTitleCell("到位车购税(万元)", false,
                    new ExcelCoordinate(0, (short) 6), null, 20,false);
            title[7] = new ExcelTitleCell("到位燃油税(万元)", false,
                    new ExcelCoordinate(0, (short) 7), null, 20,false);
            title[8] = new ExcelTitleCell("到位地方自筹(万元)", false,
                    new ExcelCoordinate(0, (short) 8), null, 20,false);
            title[9] = new ExcelTitleCell("到位厅统筹(万元)", false,
                    new ExcelCoordinate(0, (short) 9), null, 20,false);
            title[10] = new ExcelTitleCell("计划下达文号", false,
                    new ExcelCoordinate(0, (short) 10), null, 20,false);
            title[11] = new ExcelTitleCell("标段", false,
                    new ExcelCoordinate(0, (short) 11), null, 15,false);
            title[12] = new ExcelTitleCell("拨付车购税(万元)", false,
                    new ExcelCoordinate(0, (short) 12), null, 20,false, true);
            title[13] = new ExcelTitleCell("拨付燃油税(万元)", false,
                    new ExcelCoordinate(0, (short) 13), null, 20,false, true);
            title[14] = new ExcelTitleCell("拨付地方自筹(万元)", false,
                    new ExcelCoordinate(0, (short) 14), null, 20,false, true);
            title[15] = new ExcelTitleCell("拨付厅统筹(万元)", false,
                    new ExcelCoordinate(0, (short) 15), null, 20,false, true);
            title[16] = new ExcelTitleCell("拨付月份", false,
                    new ExcelCoordinate(0, (short) 16), null, 15,false, true);
            title[17] = new ExcelTitleCell("填报人", false,
                    new ExcelCoordinate(0, (short) 17), null, 20,false);
            title[18] = new ExcelTitleCell("填报时间", false,
                    new ExcelCoordinate(0, (short) 18), null, 20,false);
//            title[19] = new ExcelTitleCell("备注", false,
//                    new ExcelCoordinate(0, (short) 19), null, 50,false);
            title[19] = new ExcelTitleCell("已拨付总投资(万元)", false,
                    new ExcelCoordinate(0, (short) 19), null, 20,false);
            title[20] = new ExcelTitleCell("已拨付部补(万元)", false,
                    new ExcelCoordinate(0, (short) 20), null, 20,false);
            title[21] = new ExcelTitleCell("已拨付省补(万元)", false,
                    new ExcelCoordinate(0, (short) 21), null, 20,false);
            title[22] = new ExcelTitleCell("已拨付地方自筹(万元)", false,
                    new ExcelCoordinate(0, (short) 22), null, 20,false);

            Map<String, String> attribute = new HashMap<String, String>();
            attribute.put("0", "xmbm");//项目编码
            attribute.put("1", "xmnf");//项目年份
            attribute.put("2", "xmmc");//项目名称
            attribute.put("3", "gydw");//管养单位
            attribute.put("4", "xzqh");//行政区划
            attribute.put("5", "jhztz");//计划总投资
            attribute.put("6", "dwcgs");//计划下达车购税
            attribute.put("7", "dwrys");//计划下达燃油税
            attribute.put("8", "dwdfzc");//计划下达地方自筹
            attribute.put("9", "dwttc");//计划下达厅统筹
            attribute.put("10", "jhxdwh");//计划下达文号
            attribute.put("11", "bd");//计划下达文号
            attribute.put("12", "cgs");//车购税
            attribute.put("13", "rys");//燃油税
            attribute.put("14", "dfzc");//地方自筹
            attribute.put("15", "ttc");//厅统筹
            attribute.put("16", "bfyf");//到位月份
            attribute.put("17", "tbr");//填报人
            attribute.put("18", "tbsj");//填报时间
            attribute.put("19", "bfzj");
            attribute.put("20", "bfbb");
            attribute.put("21", "bfsb");
            attribute.put("22", "bfdf");
            List<Object> excelData = new ArrayList<Object>();

            String titleName = xmjbxx.getMbtitle()+"资金拨付模版";
            String fileName = xmjbxx.getMbtitle()+"资金拨付模版";
            xmjbxx.setXzqh(MyUtil.getQueryTJ(xmjbxx.getXzqh(), "xzqhdm"));
            xmjbxx.setXmnf(MyUtil.getQueryTJ(xmjbxx.getXmnf(), "xmnf"));
            xmjbxx.setKnw(MyUtil.getQueryTJ(xmjbxx.getKnw(), "knw"));
            xmjbxx.setJhxdwh(MyUtil.getQueryTJIN(xmjbxx.getJhxdwh(), "jhxdwh"));
            xmjbxx.setShzt(MyUtil.getQueryTJ(xmjbxx.getShzt(), "shztstr"));
            xmjbxx.setSsbzt(MyUtil.getQueryTJ(xmjbxx.getSsbzt(), "ssbztstr"));
            xmjbxx.setXsbzt(MyUtil.getQueryTJ(xmjbxx.getXsbzt(), "xsbztstr"));
            xmjbxx.setGydwdm(MyUtil.getQueryTJDW(xmjbxx.getGydwdm(), "gydwdm"));
            xmjbxx.setSfqbbf(MyUtil.getQueryTJ(xmjbxx.getSfqbbf(), "decode(zj.bfbb+zj.bfsb,null,'否',(zj.dwcgs+zj.dwsb),'是','否')"));
            List<XmZjbf> ql = fundServer.queryzjbfmb(xmjbxx);
            excelData.addAll(ql);
            ExcelEntity excel = new ExcelEntity(titleName, title, attribute,excelData);
            ExcelExportUtil.excelWritelock(excel, fileName, getresponse());
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void importCcdZjbf() {
        String fileType=fileuploadFileName.substring(fileuploadFileName.length()-3, fileuploadFileName.length());
        HttpServletResponse response = getresponse();
        PrintWriter writer = null;
        try {
            writer = response.getWriter();
            if(!"xls".equals(fileType)){
                response.getWriter().print(fileuploadFileName+"不是系统导出的excel模版文件");
                return ;
            }
            response.setCharacterEncoding("utf-8");
            FileInputStream fs = new FileInputStream(this.fileupload);
            List<Map>[] dataMapArray;
            dataMapArray = ExcelReader.readExcelContent(0,20,fs,Object.class);
            List<Map> data = ExcelReader.removeBlankRow(dataMapArray[0]);
            if(data.size()>=1) {
                if(data.get(0).get("0").equals("项目编码")&&data.get(0).get("10").equals("计划下达文号")&&data.get(0).get("11").equals("标段")&&data.get(0).get("12").equals("拨付车购税(万元)")&&data.get(0).get("13").equals("拨付燃油税(万元)")&&data.get(0).get("14").equals("拨付地方自筹(万元)")&&data.get(0).get("15").equals("拨付厅统筹(万元)")&&data.get(0).get("16").equals("拨付月份")&&data.get(0).get("17").equals("填报人")&&data.get(0).get("18").equals("填报时间"))
                    data.remove(0);
                else {
                    response.getWriter().print(fileuploadFileName+"数据有误");
                    return;
                }
            }else {
                response.getWriter().print(fileuploadFileName+"数据有误");
                return;
            }
            List<Map> resultData = new ArrayList<Map>();
            for (Map map : data) {
                map.put("SBTHCD", 11);
                if (Float.parseFloat(map.get("12").toString()) == 0f
                        && Float.parseFloat(map.get("13").toString()) == 0f
                        && Float.parseFloat(map.get("14").toString()) == 0f
                        && Float.parseFloat(map.get("15").toString()) == 0f) {
                    continue;
                }
                if (map.get("12") != null && map.get("12") != "") {
                    if (!map.get("12").toString().matches("\\d+(\\.)?\\d*")) {
                        writer.print(map.get("2") + "(" + map.get("10") +")的项目，"+"拨付车购税数据格式不对！");
                        return;
                    }
                }
                if (map.get("13") != null && map.get("13") != "") {
                    if (!map.get("13").toString().matches("\\d+(\\.)?\\d*")) {
                        writer.print(map.get("2") + "(" + map.get("10") +")的项目，"+"拨付燃油税数据格式不对！");
                        return;
                    }
                }
                if (map.get("14") != null && map.get("14") != "") {
                    if (!map.get("14").toString().matches("\\d+(\\.)?\\d*")) {
                        writer.print(map.get("2") + "(" + map.get("10") +")的项目，"+"拨付地方自筹数据格式不对！");
                        return;
                    }
                }
                if (map.get("15") != null && map.get("15") != "") {
                    if (!map.get("15").toString().matches("\\d+(\\.)?\\d*")) {
                        writer.print(map.get("2") + "(" + map.get("10") +")的项目，"+"拨付厅统筹数据格式不对！");
                        return;
                    }
                }
                resultData.add(map);
            }
            if (resultData.size() == 0) {
                writer.print("导入失败, 您导入的项目拨付资金全为0, 不允许保存！");
                return;
            }
            JhcxServerImpl jhcx=new JhcxServerImpl();
            Xmjbxx x=new Xmjbxx();
            Xmjbxx x1=new Xmjbxx();
            Xmjbxx x2=new Xmjbxx();
            Xmjbxx x3=new Xmjbxx();
            for(int i=0;i<resultData.size();i++){
                // 项目编码
                x.setXmbm(resultData.get(i).get("0").toString());
                // 计划下达文号
                x.setJhxdwh(resultData.get(i).get("10").toString());
                x1=jhcx.getbfByWh(x);
                x3=jhcx.getdwByWh(x);
                x2=jhcx.getJhxdByWh2(x);
                if(x1==null){
                    x1=new Xmjbxx();
                    x1.setDwzj("0");
                    x1.setDwzbz("0");
                }
                if(x2==null){
                    x2=new Xmjbxx();
                    x2.setDwzj("0");
                    x2.setDwzbz("0");
                }
                if (x3 == null) {
                    x3=new Xmjbxx();
                    x3.setDwzj("0");
                    x3.setDwzbz("0");
                }
                if(Double.valueOf(x3.getDwzj())<Double.valueOf(x1.getDwzj())
                        +Double.valueOf(resultData.get(i).get("12").toString())
                        +Double.valueOf(resultData.get(i).get("13").toString())
                        +Double.valueOf(resultData.get(i).get("14").toString())
                        +Double.valueOf(resultData.get(i).get("15").toString())
                ){
                    response.getWriter().print(fileuploadFileName+"导入失败,第"+(i+2)+"行数据所填总投资大于资金到位总投资，不允许保存！");
                    return;
                }
                if(Double.valueOf(x3.getDwzbz())<Double.valueOf(x1.getDwzbz())
                        +Double.valueOf(resultData.get(i).get("12").toString())
                        +Double.valueOf(resultData.get(i).get("13").toString())
                        +Double.valueOf(resultData.get(i).get("15").toString())
                ){
                    response.getWriter().print(fileuploadFileName+"导入失败,第"+(i+2)+"行数据所填总补助大于资金到位总补助，不允许保存！");
                    return;
                }

                if(Double.valueOf(x2.getDwzj())<Double.valueOf(x1.getDwzj())
                        +Double.valueOf(resultData.get(i).get("12").toString())
                        +Double.valueOf(resultData.get(i).get("13").toString())
                        +Double.valueOf(resultData.get(i).get("14").toString())
                        +Double.valueOf(resultData.get(i).get("15").toString())
                ){
                    response.getWriter().print(fileuploadFileName+"导入失败,第"+(i+2)+"行数据所填总投资大于计划下达总投资，不允许保存！");
                    return;
                }
                if(Double.valueOf(x2.getDwzbz())<Double.valueOf(x1.getDwzbz())
                        +Double.valueOf(resultData.get(i).get("12").toString())
                        +Double.valueOf(resultData.get(i).get("13").toString())
                        +Double.valueOf(resultData.get(i).get("15").toString())
                ){
                    response.getWriter().print(fileuploadFileName+"导入失败,第"+(i+2)+"行数据所填总补助大于计划下达总补助，不允许保存！");
                    return;
                }
            }
            //将数据插入到数据库
            boolean b=fundServer.importZjbf(resultData);
            if(b) {
                writer.print(fileuploadFileName+"导入成功");
            } else {
                writer.print(fileuploadFileName+"导入失败");
            }
        } catch (Exception e) {
            writer.print(fileuploadFileName+"数据有误");
            return;
        } finally {
            writer.close();
        }
    }

    public void importCcdZjdw() {
        String fileType=fileuploadFileName.substring(fileuploadFileName.length()-3, fileuploadFileName.length());
        HttpServletResponse response = getresponse();
        PrintWriter writer = null;
        try {
            writer = response.getWriter();
            if(!"xls".equals(fileType)){
                writer.print(fileuploadFileName+"不是系统导出的excel模版文件");
                return ;
            }
            response.setCharacterEncoding("utf-8");
            FileInputStream fs = new FileInputStream(this.fileupload);
            List<Map>[] dataMapArray;
            dataMapArray = ExcelReader.readExcelContent(0,22,fs,Object.class);
            List<Map> data = ExcelReader.removeBlankRow(dataMapArray[0]);
            if(data.size()>=1) {
                if(data.get(0).get("0").equals("项目编码")&&data.get(0).get("10").equals("计划下达文号")&&data.get(0).get("11").equals("到位车购税(万元)")&&data.get(0).get("12").equals("到位燃油税(万元)")&&data.get(0).get("13").equals("到位地方自筹(万元)")&&data.get(0).get("14").equals("到位厅统筹(万元)")&&data.get(0).get("15").equals("到位月份")&&data.get(0).get("16").equals("填报人")&&data.get(0).get("17").equals("填报时间"))
                    data.remove(0);
                else {
                    writer.print(fileuploadFileName+"数据有误");
                    return;
                }
            }else {
                writer.print(fileuploadFileName+"数据有误");
                return;
            }
            List<Map> resultData = new ArrayList<Map>();
            JhcxServerImpl jhcx=new JhcxServerImpl();
            Xmjbxx x=new Xmjbxx();
            Xmjbxx x1=new Xmjbxx();
            Xmjbxx x2=new Xmjbxx();
            int count = -1;
            for (Map map : data) {
                map.put("SBTHCD", 11);
                if (map.get("11") != null && map.get("11") != "") {
                    if (!map.get("11").toString().matches("\\d+(\\.)?\\d*")) {
                        writer.print(map.get("2") + "(" + map.get("10") +")的项目，"+"到位车购税数据格式不对！");
                        return;
                    }
                }
                if (map.get("12") != null && map.get("12") != "") {
                    if (!map.get("12").toString().matches("\\d+(\\.)?\\d*")) {
                        writer.print(map.get("2") + "(" + map.get("10") +")的项目，"+"到位燃油税数据格式不对！");
                        return;
                    }
                }
                if (map.get("13") != null && map.get("13") != "") {
                    if (!map.get("13").toString().matches("\\d+(\\.)?\\d*")) {
                        writer.print(map.get("2") + "(" + map.get("10") +")的项目，"+"到位地方自筹数据格式不对！");
                        return;
                    }
                }
                if (map.get("14") != null && map.get("14") != "") {
                    if (!map.get("14").toString().matches("\\d+(\\.)?\\d*")) {
                        writer.print(map.get("2") + "(" + map.get("10") +")的项目，"+"到位厅统筹数据格式不对！");
                        return;
                    }
                }
                count++;

                if (Float.parseFloat(map.get("11").toString()) == 0f
                        && Float.parseFloat(map.get("12").toString()) == 0f
                        && Float.parseFloat(map.get("13").toString()) == 0f
                        && Float.parseFloat(map.get("14").toString()) == 0f) {
                    continue;
                }

                // 项目编码
                x.setXmbm(map.get("0").toString());
                // 计划下达文号
                x.setJhxdwh(map.get("10").toString());
                x1=jhcx.getdwByWh(x);
                x2=jhcx.getJhxdByWh2(x);

                if(Double.valueOf(x2.getDwzj())<Double.valueOf(x1.getDwzj())
                        +Double.valueOf(map.get("11").toString())
                        +Double.valueOf(map.get("12").toString())
                        +Double.valueOf(map.get("13").toString())
                        +Double.valueOf(map.get("14").toString())
                ){
                    response.getWriter().print(fileuploadFileName+"导入失败,第"+(count+2)+"行数据所填总投资大于计划下达总投资，不允许保存！");
                    return;
                }
                if(Double.valueOf(x2.getDwzbz())<Double.valueOf(x1.getDwzbz())
                        +Double.valueOf(map.get("11").toString())
                        +Double.valueOf(map.get("12").toString())
                        +Double.valueOf(map.get("14").toString())
                ){
                    response.getWriter().print(fileuploadFileName+"导入失败,第"+(count+2)+"行数据所填总补助大于计划下达总补助，不允许保存！");
                    return;
                }
//                XmZjdw zjdwData = fundServer.queryZjdwDataByXmid(map);
//                double ydwzj = 0d;
//                if (zjdwData != null) {
//                    ydwzj = new BigDecimal(zjdwData.getZtz())
//                            .add(new BigDecimal(map.get("11").toString()))
//                            .add(new BigDecimal(map.get("12").toString()))
//                            .add(new BigDecimal(map.get("13").toString()))
//                            .add(new BigDecimal(map.get("14").toString()))
//                            .doubleValue();
//                } else {
//                    ydwzj = new BigDecimal(map.get("11").toString())
//                            .add(new BigDecimal(map.get("12").toString()))
//                            .add(new BigDecimal(map.get("13").toString()))
//                            .add(new BigDecimal(map.get("14").toString()))
//                            .doubleValue();
//                }
//                if (ydwzj > Double.parseDouble(map.get("5").toString())) {
//                    writer.print(map.get("2") + "(" + map.get("10") +")的项目，"+"所填总投资大于计划下达总投资, 不允许保存！");
//                    return;
//                }
                resultData.add(map);
            }

            if (resultData.size() == 0) {
                writer.print("导入失败, 您导入的项目到位资金全为0, 不允许保存！");
                return;
            }
            //将数据插入到数据库
            boolean b = fundServer.importZjdw(resultData);
            if(b) {
                writer.print(fileuploadFileName+"导入成功");
            } else {
                ResponseUtils.write(getresponse(),  fileuploadFileName+"导入失败");
            }
        } catch (Exception e) {
            writer.print(fileuploadFileName+"数据有误");
            return;
        }
    }

    public void ccdDcmb() {
        ExcelTitleCell[] title = null;
        try {
            title = new ExcelTitleCell[22];
            title[0] = new ExcelTitleCell("项目编码", false,
                    new ExcelCoordinate(0, (short) 0), null, 10,false);
            title[1] = new ExcelTitleCell("项目年份", false,
                    new ExcelCoordinate(0, (short) 1), null, 20,false);
            title[2] = new ExcelTitleCell("项目名称", false,
                    new ExcelCoordinate(0, (short) 2), null, 20,false);
            title[3] = new ExcelTitleCell("管养单位", false,
                    new ExcelCoordinate(0, (short) 3), null, 20,false);
            title[4] = new ExcelTitleCell("行政区划", false,
                    new ExcelCoordinate(0, (short) 4), null, 20,false);
            title[5] = new ExcelTitleCell("计划下达资金(万元)", false,
                    new ExcelCoordinate(0, (short) 5), null, 20,false);
            title[6] = new ExcelTitleCell("计划下达车购税(万元)", false,
                    new ExcelCoordinate(0, (short) 6), null, 20,false);
            title[7] = new ExcelTitleCell("计划下达燃油税(万元)", false,
                    new ExcelCoordinate(0, (short) 7), null, 20,false);
            title[8] = new ExcelTitleCell("计划下达地方自筹(万元)", false,
                    new ExcelCoordinate(0, (short) 8), null, 20,false);
            title[9] = new ExcelTitleCell("计划下达厅统筹(万元)", false,
                    new ExcelCoordinate(0, (short) 9), null, 20,false);
            title[10] = new ExcelTitleCell("计划下达文号", false,
                    new ExcelCoordinate(0, (short) 10), null, 20,false);
            title[11] = new ExcelTitleCell("到位车购税(万元)", false,
                    new ExcelCoordinate(0, (short) 11), null, 20,false, true);
            title[12] = new ExcelTitleCell("到位燃油税(万元)", false,
                    new ExcelCoordinate(0, (short) 12), null, 20,false, true);
            title[13] = new ExcelTitleCell("到位地方自筹(万元)", false,
                    new ExcelCoordinate(0, (short) 13), null, 20,false, true);
            title[14] = new ExcelTitleCell("到位厅统筹(万元)", false,
                    new ExcelCoordinate(0, (short) 14), null, 20,false, true);
            title[15] = new ExcelTitleCell("到位月份", false,
                    new ExcelCoordinate(0, (short) 15), null, 15,false, true);
            title[16] = new ExcelTitleCell("填报人", false,
                    new ExcelCoordinate(0, (short) 16), null, 20,false);
            title[17] = new ExcelTitleCell("填报时间", false,
                    new ExcelCoordinate(0, (short) 17), null, 20,false);
            title[18] = new ExcelTitleCell("已到位总投资(万元)", false,
                    new ExcelCoordinate(0, (short) 18), null, 20,false);
            title[19] = new ExcelTitleCell("已到位部补(万元)", false,
                    new ExcelCoordinate(0, (short) 19), null, 20,false);
            title[20] = new ExcelTitleCell("已到位省补(万元)", false,
                    new ExcelCoordinate(0, (short) 20), null, 20,false);
            title[21] = new ExcelTitleCell("已到位地方自筹(万元)", false,
                    new ExcelCoordinate(0, (short) 21), null, 20,false);

            Map<String, String> attribute = new HashMap<String, String>();
            attribute.put("0", "xmbm");//项目编码
            attribute.put("1", "xmnf");//项目年份
            attribute.put("2", "xmmc");//项目名称
            attribute.put("3", "gydw");//管养单位
            attribute.put("4", "xzqh");//行政区划
            attribute.put("5", "jhztz");//计划总投资
            attribute.put("6", "xdcgs");//计划下达车购税
            attribute.put("7", "xdrys");//计划下达燃油税
            attribute.put("8", "xddfzc");//计划下达地方自筹
            attribute.put("9", "xdttc");//计划下达厅统筹
            attribute.put("10", "jhxdwh");//计划下达文号
            attribute.put("11", "cgs");//车购税
            attribute.put("12", "rys");//燃油税
            attribute.put("13", "dfzc");//地方自筹
            attribute.put("14", "ttc");//厅统筹
            attribute.put("15", "dwyf");//到位月份
            attribute.put("16", "tbr");//填报人
            attribute.put("17", "tbsj");//填报时间
            attribute.put("18", "dwzj");
            attribute.put("19", "dwbb");
            attribute.put("20", "dwsb");
            attribute.put("21", "dwdf");

            List<Object> excelData = new ArrayList<Object>();
            String titleName = xmjbxx.getMbtitle()+"资金到位模版";
            String fileName = xmjbxx.getMbtitle()+"资金到位模版";
            xmjbxx.setXzqh(MyUtil.getQueryTJ(xmjbxx.getXzqh(), "xzqhdm"));
            xmjbxx.setXmnf(MyUtil.getQueryTJ(xmjbxx.getXmnf(), "xmnf"));

            xmjbxx.setJhxdwh(MyUtil.getQueryTJIN(xmjbxx.getJhxdwh(), "jhxdwh"));
            xmjbxx.setTsdq(MyUtil.getQueryTJ(xmjbxx.getTsdq(), "tsdq"));
            xmjbxx.setGcfl(MyUtil.getQueryTJ(xmjbxx.getGcfl(), "gcfl"));
            xmjbxx.setShzt(MyUtil.getQueryTJ(xmjbxx.getShzt(), "shztstr"));
            xmjbxx.setSsbzt(MyUtil.getQueryTJ(xmjbxx.getSsbzt(), "ssbztstr"));
            xmjbxx.setXsbzt(MyUtil.getQueryTJ(xmjbxx.getXsbzt(), "xsbztstr"));
            xmjbxx.setKnw(MyUtil.getQueryTJ(xmjbxx.getKnw(), "knw"));
            xmjbxx.setGydwdm(MyUtil.getQueryTJDW(xmjbxx.getGydwdm(), "gydwdm"));
            xmjbxx.setSfqbdw(MyUtil.getQueryTJ(xmjbxx.getSfqbdw(), "decode(zj.dwbb+zj.dwsb,null,'否',(zj.jhxdcgs+zj.jhxdttc),'是','否')"));
            List<XmZjdw> ql = fundServer.queryzjdwmb(xmjbxx);
            excelData.addAll(ql);
            ExcelEntity excel = new ExcelEntity(titleName, title, attribute,excelData);
            ExcelExportUtil.excelWritelock(excel, fileName, getresponse());
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void getJsxzTreeByName() {
        try {
            List<TreeNode> list = fundServer.getJsxzTreeByName();
            JsonUtils.write(list, getresponse().getWriter());
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public FundBean getModel() {
        return fundBean;
    }
}
